/*
 * Copyright (C) 2009 Foregone.
 * All Rights Reserved. The following is Source Code and is subject to all restrictions
 * on such code as contained in the End User License Agreement accompanying this product.
 */

package org.foregone.kwicks.kwicksMenu;

import com.google.gwt.dom.client.HeadingElement;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;

/**
 * User: Thil Bandara
 * Date: Oct 18, 2009
 * Version: 0.5
 * - Add background image
 * - Store item width animation percentages
 * - Link url to support anchor tag
 */
public class KwicksItem extends ComplexPanel implements HasMouseOverHandlers, HasMouseOutHandlers, HasClickHandlers, HasText {

  String linkURL;
  int position;
  int initialWidth = 0;
  String imagePath;

  public KwicksItem(String heading, String description, String imagePath, String anchorTag) {
    Element base = DOM.createElement("li");
    base.setClassName("kwicks-li");
    setElement(base);
    setText(heading);
    if (description != null) {
      setDescription(description);
    }
    this.linkURL = anchorTag;
    this.imagePath = imagePath;
  }

  /**
   * Set the background image of the element
   * @param x background x offset position
   * @param y background y offset position
   */
  public void setBackgroundImage( int x, int y ){
    if (imagePath != null) {
      DOM.setStyleAttribute(this.getElement(), "background", "url('" + imagePath + "') no-repeat " + (  initialWidth + x ) + "px " + y + "px");
    }    
  }

  public void add(Widget w) {
    super.add(w, getElement());
  }

  public void setDescription(String description) {
    HeadingElement he = HeadingElement.as(DOM.createElement("h3"));
    he.setInnerText(description);
    this.getElement().appendChild(he);
  }

  public String getText() {
    return DOM.getInnerText(getElement());
  }

  public void setText(String text) {
    DOM.setInnerText(getElement(), (text == null) ? "" : text);
  }

  public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
    return addDomHandler(handler, MouseOutEvent.getType());
  }

  public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
    return addDomHandler(handler, MouseOverEvent.getType());
  }

  public HandlerRegistration addClickHandler(ClickHandler handler) {
    return addDomHandler(handler, ClickEvent.getType());
  }
}
